package cn.xyf.mzmy.common.pojo;

import cn.hutool.core.lang.Assert;
import cn.xyf.mzmy.common.enums.GlobalErrorCodeConstants;
import cn.xyf.mzmy.common.exception.ErrorCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * 统一结果返回类
 * @param <T>
 */


@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Accessors(chain = true)
public class Result<T> {

    //响应状态码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;

    public static <T> Result<T> ok(){
        return new Result<>(GlobalErrorCodeConstants.SUCCESS.getCode(),null,null);
    }

    public static <T> Result<T> ok(T data){
        return new Result<>(GlobalErrorCodeConstants.SUCCESS.getCode(), null, data);
    }

    public static <T> Result<T> ok(T data,String message){
        return new Result<>(GlobalErrorCodeConstants.SUCCESS.getCode(), message, data);
    }

    public static <T> Result<T> fail(String errorMsg){
        return new Result<>(GlobalErrorCodeConstants.DEFAULT_ERROR.getCode(), errorMsg, null);
    }

    public static <T> Result<T> fail(Integer code, String message) {
        Assert.notEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), code, "code 必须是错误的！");
        Result<T> result = new Result<>();
        result.code = code;
        result.message = message;
        return result;
    }

    public static <T> Result<T> fail(ErrorCode errorCode) {
        return fail(errorCode.getCode(), errorCode.getMsg());
    }

}
